/**
 * 王栋
 * 功能点：vue中防止用户在短时间内频繁多次点击按钮的解决办法
 */
import Vue from 'vue'
const preventReClick = Vue.directive('preventReClick', {
  inserted(el, binding) {
    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true
        setTimeout(() => {
          el.disabled = false
        }, binding.value || 1000)
      }
    })
  }
})

const cusTooltip = Vue.directive('cusTooltip',{
  inserted: (el, binding) => {

       const elComputed = document.defaultView.getComputedStyle(el, '');
       const padding =
         parseInt(elComputed.paddingLeft.replace('px', '')) + parseInt(elComputed.paddingRight.replace('px', ''));
   
       const range = document.createRange();
       range.setStart(el, 0);
       range.setEnd(el, el.childNodes.length);
       const rangeWidth = range.getBoundingClientRect().width;
   
       if (rangeWidth + padding > el.offsetWidth || el.scrollWidth > el.offsetWidth) {
         el.title = binding.value;
       }
     
  }
})


export {
  preventReClick,
  cusTooltip,
}

